package cn.iocoder.yudao.module.iot.gateway.protocol.custom.xiaoan.tcp.codec.parser.cmd;

import cn.hutool.core.map.MapUtil;
import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
import cn.iocoder.yudao.module.iot.gateway.protocol.custom.xiaoan.tcp.codec.XiaoAnCmdTypeEnum;
import cn.iocoder.yudao.module.iot.gateway.protocol.custom.xiaoan.tcp.codec.parser.XiaoAnMessageParser;
import io.vertx.core.buffer.Buffer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class HeartbeatMessageParser implements XiaoAnMessageParser {
    @Override
    public boolean supports(XiaoAnCmdTypeEnum cmd) {
        return XiaoAnCmdTypeEnum.PING.equals(cmd);
    }

    @Override
    public IotDeviceMessage parse(Buffer buffer, int index, byte cmd, byte seq) {
        // 设备的GSM 信号强度，取值范围[0,31]
        int csq = buffer.getByte(index);
        index++;

        // 设备实时电压，单位：伏
        int voltage = buffer.getByte(index);
        index++;

        log.info("[parse][csq: {}, voltage: {}]", csq, voltage);
        return IotDeviceMessage.requestOf(String.valueOf(seq), String.valueOf(cmd),
                MapUtil.builder()
                        .put("csq", csq).put("voltage", voltage)
        );
    }
}
